import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/generated/json/base/json_convert_content.dart';
import 'package:yurun_smartmerchants/models/modify_staff_entity.dart';
import 'package:yurun_smartmerchants/models/staff_list_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/routes/routes.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:yurun_smartmerchants/utils/evenbus_utils.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';

class ModifyStaffPage extends StatefulWidget {

  final StaffListContent staff;

  ModifyStaffPage({this.staff}); //  ModifyStaffPage(this.name);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ModifyStaffPageState();
  }
}

class _ModifyStaffPageState extends State<ModifyStaffPage> {

  TextEditingController editController = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    editController.text = widget.staff.name;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colours.white,
        centerTitle: true,
        iconTheme: IconThemeData(color: Colours.app_words),
        title: Text(
          '修改员工',
          style: TextStyle(fontSize: 18, color: Colours.app_words),
        ),
      ),
      resizeToAvoidBottomPadding: false,
      body: Container(
        color: Colours.app_background,
        child: Column(
          children: <Widget>[
            Expanded(
                child: Container(
                  child: Column(
                    children: <Widget>[
                      Container(
                        color: Colors.white,
                        margin: EdgeInsets.only(top: AppSize.height(20)),
                        height: AppSize.height(98),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(left: AppSize.width(32)),
                                  child: Text(
                                    '员工姓名',
                                    style: TextStyle(
                                        fontSize: AppSize.sp(32),
                                        color: Colours.color_313131),
                                  ),
                                )),
                            Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(right: AppSize.width(32)),
                                  child: TextField(
                                    controller: editController,
                                    textAlign: TextAlign.right,
                                    maxLength: 4,
                                    style: TextStyle(
                                        fontSize: AppSize.sp(32),
                                        color: Colours.color_313131),
                                    keyboardType: TextInputType.text,
                                    decoration: InputDecoration(
                                      counterText: '',
                                      border: InputBorder.none,
                                      hintText: '输入姓名',
                                      hintStyle: TextStyle(
                                          fontSize: AppSize.sp(32),
                                          color: Colours.color_abacb5),
                                    ),
                                  ),
                                )),
                          ],
                        ),
                      )
                    ],
                  ),
                )
            ),
            Container(
              height: AppSize.height(135),
              color: Colors.white,
              alignment: Alignment.center,
              child: Container(
                height: AppSize.height(90),
                width: double.infinity,
                margin: EdgeInsets.only(
                    left: AppSize.width(20), right: AppSize.width(20)),
                child: RaisedButton(
                  onPressed: () {
                    _modifyStaff();
                  },
                  color: Colours.app_main,
                  child: Text(
                    '保存',
                    style: TextStyle(
                        fontSize: AppSize.sp(32), color: Colours.white),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void _modifyStaff(){
    String name = editController.text;
    int id = widget.staff.id;
    if(name == null || name.isEmpty){
      showToast('员工姓名不能为空~');
      return;
    }
    LoadingUtil.showLoading(context);
    ApiInterface.modifyStaff(id, name).then((value){
      ModifyStaffEntity modifyStaffEntity = JsonConvert.fromJsonAsT<ModifyStaffEntity>(value);
      bool success = modifyStaffEntity.success;
      if(success){
        showToast('修改成功');
        EventBus().send('refresh_staff','');
      }

    }).whenComplete((){
      LoadingUtil.hideLoading(context);
      Navigator.of(context).popUntil(ModalRoute.withName(staff_management_page));
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    editController.dispose();
  }
}
